﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PokerClient
{
    public partial class FormPortal : Form
    {
        FormLogin formLogin;
        FormGame formGame;
        private ServiceReference1.PortalClient proxy;
        public FormPortal()
        {
            InitializeComponent();
            formLogin = new FormLogin();
            formLogin.ShowDialog();
            proxy = new ServiceReference1.PortalClient();
        }

        private void toolStripTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Only numbers allow in toolStripTextBox1
            e.Handled = !char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar);
        }

        private void FormPortal_Load(object sender, EventArgs e)
        {
            usernameLable.Text = ("Username: " +formLogin.LoginUsername);
            balanceLable.Text = ("Balance: " +Convert.ToString(proxy.GetPlayerMoney(formLogin.LoginUsername)));
        }

        private void TopUpBtn_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(toolStripTextBox1.Text))
            {
                proxy.TopUpMoney(formLogin.LoginUsername, Convert.ToInt32(toolStripTextBox1.Text));
                balanceLable.Text = ("Balance: " + Convert.ToString(proxy.GetPlayerMoney(formLogin.LoginUsername)));
                toolStripTextBox1.Clear();
            }
        }

        private void ReloadTablesBtn_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            foreach (var i in proxy.GetTableList())
            {
                listBox1.Items.Add("Table ID: " +i.TableID + " Table name: " + i.Name + " Stackes: " + i.Stackes 
                    + " Min Buy In: " + i.MinBuyIn + " Number of Players: " + i.PlayingList.Count() + "/8");
            };
        }

        private void CreateTableBtn_Click(object sender, EventArgs e)
        {
            proxy.CreateTable(textBox1.Text, Convert.ToInt32(numericUpDown2.Value), Convert.ToInt32(numericUpDown1.Value));
            ReloadTablesBtn.PerformClick();
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int index = this.listBox1.IndexFromPoint(e.Location);
            if (index != System.Windows.Forms.ListBox.NoMatches)
            {
                int tableid = index + 1;
                formGame = new FormGame(tableid, formLogin.LoginUsername);
                formGame.ShowDialog();
            }
        }
    }
}
